
"""
 * 341. 扁平化嵌套列表迭代器
 * 思路：将NestedIterator看成一个N叉树，可以将该题转换成遍历N叉树的过程
 * [[1,1],2,[1,1]]
"""
from typing import Optional

from com.siyuan.part34 import NestedInteger


class NestedIterator:

    def __init__(self, nestedList: [NestedInteger]):
        self.list: NestedInteger = []
        for item in nestedList:
            self.traverse(item)


    def next(self) -> int:
        return self.list.pop()

    def hasNext(self) -> bool:
        return len(self.list) > 0

    '''遍历N叉树'''
    def traverse(self, root: Optional[NestedInteger]):
        if root.isInteger():
            self.list.append(root.getInteger())
            return
        for item in root.getList():
            self.traverse(item)